package de.homer.persistence.user;

import java.util.Collection;

import org.springframework.stereotype.Repository;

import de.homer.core.user.domain.User;
import de.homer.persistence.AbstractRepositoryJPA;
import de.homer.persistence.exceptions.UserNotFoundException;

/**
 * An repository for {@link User}.
 * 
 * @author André Stannek
 * 
 */
@Repository
public class UserRepositoryJPA extends AbstractRepositoryJPA<User> implements
		UserRepository {

	/**
	 * 
	 */
	private static final long serialVersionUID = 763537504534424989L;

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.homer.core.repository.Repository#getAll()
	 */
	@Override
	public final Collection<User> getAll() {
		return getAll(User.class);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * de.homer.core.user.repository.UserRepository#getUser(java.lang.String)
	 */
	@Override
	public final User getUser(final String username) throws UserNotFoundException {
		Collection<User> users = getAll();
		for (User user : users) {
			if (user.getUsername().equals(username)) {
				return user;
			}
		}
		throw new UserNotFoundException("User with name " + username
				+ " not found in database!");
	}
}
